package com.qwer.controller.accesscontrol;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qwer.controller.comm.BaseController;
import com.qwer.model.accesscontrol.AccApiInfo;
import com.qwer.model.accesscontrol.AccControlMapInfo;
import com.qwer.model.accesscontrol.AccFileInfo;
import com.qwer.service.accesscontrol.AccApiService;
import com.qwer.service.accesscontrol.AccControlMapService;
import com.qwer.service.accesscontrol.AccFileService;
import com.qwer.util.EncryptionHelper;

@Controller
@RequestMapping("/accTest")
public class AccTestController extends BaseController{
    
	@Autowired
    private AccApiService accApiService;
	
	@Autowired
    private AccFileService accFileService;
	
	@Autowired
    private AccControlMapService accControlMapService;
    
    
    
	
	@RequestMapping(value = { "/edit"})
    public String zpIndex(Integer id,Model model) {
        AccApiInfo data = new AccApiInfo();
        if(id!=null){
            data = accApiService.findById(id);
        }
        model.addAttribute("model", data);
        return "page/accesscontrol/accApi/edit";
    }
	
    
    @RequestMapping(value = { "/testapi"})
    public String testapi(Model model,AccControlMapInfo acm,HttpServletRequest req) {
        
    	JSONObject jobj = new JSONObject();
        
    	acm.setCallUserId(getLoginUser(req).getId());
        List<AccControlMapInfo> list = accControlMapService.findByCallUserIdAndCallIdAndType(acm);
        
        Integer code = -1;
        String msg = "没有授权，不能访问！";
        if(list.size()>0){
            msg="已被授权，可以正常访问！";
            code=0;
            List<AccApiInfo> data  = accApiService.findByUserId(acm.getUserId());
            jobj.put("data",JSON.toJSONString(data));
            model.addAttribute("data", new EncryptionHelper("1AA888BV9IUahhhY").encrypt(JSON.toJSONString(data)));
        }
        
        model.addAttribute("code", code);
        model.addAttribute("msg", msg);
        
        
        return "page/accesscontrol/accApi/testRes";
        
    }
    
    
    @RequestMapping(value = { "/testfile"})
    public String testfile(Model model,AccControlMapInfo acm,HttpServletRequest req) {
        
        model.addAttribute("href", "/web_seach/accTest/seekExperts?id="+acm.getCallId());
        
        return "page/accesscontrol/accFile/testRes";
        
    }
    
    @RequestMapping(value = { "/decrypt"})
    @ResponseBody
    public String decrypt(String text) {
        
        return new EncryptionHelper("1AA888BV9IUahhhY").decrypt(text);
        
    }
    
    
    @RequestMapping(value = "/seekExperts")
	@ResponseBody
	public String createFolw(HttpServletRequest request,
			HttpServletResponse response, Model model,Integer id) {
		// response.setContentType("image/*");
		FileInputStream fis = null;
		OutputStream os = null;
		try {
			AccFileInfo m =accFileService.findById(id);
			fis = new FileInputStream(m.getFtpPath());
			os = response.getOutputStream();
			int count = 0;
			byte[] buffer = new byte[1024 * 8];
			while ((count = fis.read(buffer)) != -1) {
				os.write(buffer, 0, count);
				os.flush();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			fis.close();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return "ok";
	}

    
    
    
}
